package top.fzqblog.music.util;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import top.fzqblog.music.event.SongChangeEvent;
import top.fzqblog.music.model.Song;

import static top.fzqblog.music.util.Constants.TAG;

public class MusicPlayer implements MediaPlayer.OnCompletionListener {

    private static MusicPlayer player = new MusicPlayer();

    private MediaPlayer mMediaPlayer ;
    private Context mContext;
    private List<Song> mQueue;
    private int mQueueIndex;
    private PlayMode mPlayMode;


    public List<Song> getmQueue(){
        return mQueue;
    }


    private enum PlayMode {
        LOOP, RANDOM, REPEAT
    }

    public int getmQueueIndex(){
        return mQueueIndex%mQueue.size();
    }

    public static MusicPlayer getPlayer() {
        return player;
    }



    public static void setPlayer(MusicPlayer player) {
        MusicPlayer.player = player;
    }

    public MusicPlayer() {
        mMediaPlayer = new ManagedMediaPlayer();
        mMediaPlayer.setOnCompletionListener(this);
        mQueue = new ArrayList<>();
        mQueueIndex = 0;
        mPlayMode = PlayMode.LOOP;
    }

    public void setQueue(List<Song> queue, int index) {
        mQueue = queue;
        mQueueIndex = index;
        play(getNowPlaying());
    }

    public void play(Song song) {
        try {
            mMediaPlayer.reset();//重置后才能重新设置播放源
            Log.i(TAG, "play: " + song);
            Log.i(TAG, "songs: " + mQueue);
            mMediaPlayer.setDataSource(song.getPath());
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        mMediaPlayer.pause();
    }

    public void resume() {
        mMediaPlayer.start();
    }

    public void next() {
        play(getNextSong());
        EventBus.getDefault().post(new SongChangeEvent(mQueue.get(mQueueIndex), 3));
    }

    public void previous() {
        play(getPreviousSong());
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        next();
    }

    /**
     * 获取当前播放歌曲
     * @return
     */
    private Song getNowPlaying() {
        if (mQueue.isEmpty()) {
            return null;
        }
        return mQueue.get(mQueueIndex);
    }

    /**
     * 获取下一首
     * @return
     */
    private Song getNextSong() {
        if (mQueue.isEmpty()) {
            return null;
        }
        switch (mPlayMode) {
            case LOOP:
                mQueueIndex++;
                return mQueue.get(getNextIndex());
            case RANDOM:
                return mQueue.get(getRandomIndex());
            case REPEAT:
                return mQueue.get(mQueueIndex);
        }
        return null;
    }

    /**
     * 获取前一首
     * @return
     */
    private Song getPreviousSong() {
        if (mQueue.isEmpty()) {
            return null;
        }
        switch (mPlayMode) {
            case LOOP:
                return mQueue.get(getPreviousIndex());
            case RANDOM:
                return mQueue.get(getRandomIndex());
            case REPEAT:
                return mQueue.get(mQueueIndex);
        }
        return null;
    }


    /**
     * 获取当前播放曲目索引
     * @return
     */
    public int getCurrentPosition() {
        if (getNowPlaying() != null) {
            return mMediaPlayer.getCurrentPosition();
        }
        return 0;
    }

    /**
     * 播放时长获取
     * @return
     */
    public int getDuration() {
        if (getNowPlaying() != null) {
            return mMediaPlayer.getDuration();
        }
        return 0;
    }

    /**
     * 获取播放模式
     * @return
     */
    public PlayMode getPlayMode() {
        return mPlayMode;
    }


    /**
     * 设置播放模式
     * @param playMode
     */
    public void setPlayMode(PlayMode playMode) {
        mPlayMode = playMode;
    }


    /**
     * 获取下一首
     * @return
     */
    private int getNextIndex() {
        mQueueIndex = (mQueueIndex) % mQueue.size();
        return mQueueIndex;
    }

    /**
     * 获取前一首
     * @return
     */
    private int getPreviousIndex() {
        mQueueIndex = (mQueueIndex - 1) % mQueue.size();
        return mQueueIndex;
    }

    /**
     * 获取随机播放索引
     * @return
     */
    private int getRandomIndex() {
        mQueueIndex = new Random().nextInt(mQueue.size()) % mQueue.size();
        return mQueueIndex;
    }


    /**
     * 释放MediaPlayer资源
     */
    private void release() {
        mMediaPlayer.release();
        mMediaPlayer = null;
        mContext = null;
    }

}